package example.tallydemo.MyActivity;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioGroup;

import java.util.Calendar;

import example.tallydemo.MyTools.MyTimeTool;
import example.tallydemo.R;

public class AddBookActivity extends AppCompatActivity
{

    String Date_str;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);

        button = findViewById(R.id.id_setData);
        button.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                setDate(v);
            }
        });
    }
    public void doAddOk(View view)
    {
        Boolean type = readUserInput();//账本类型
        String origin = ((EditText)findViewById(R.id.id_origin_book)).getText().toString(); //账本来源
        String classify = ((EditText)findViewById(R.id.id_classify_book)).getText().toString(); //账本分类
        String money = ((EditText)findViewById(R.id.id_monery_book)).getText().toString(); //账本金额
        String introduction = ((EditText)findViewById(R.id.id_introduction_book)).getText().toString(); //账本简介
        Intent intent = new Intent();
        intent.putExtra("type",type);
        intent.putExtra("date",Date_str);
        intent.putExtra("origin",origin);
        intent.putExtra("classify",classify);
        intent.putExtra("money",money);
        intent.putExtra("introduction",introduction);
        setResult(RESULT_OK,intent);
        finish();
    }

    public Boolean readUserInput()
    {
        Boolean flag = false;
        RadioGroup radioGroup = findViewById(R.id.id_radioGroup);
        int id = radioGroup.getCheckedRadioButtonId();
        if (id == R.id.id_input_button)
        {
            flag = true;
        }
        else if(id == R.id.id_expend_button)
        {
            flag = false;
        }
        return flag;
    }

    public void setDate(View view)
    {
        Calendar calendar = Calendar.getInstance();
        new DatePickerDialog(this, R.style.MyDatePickerDialogTheme, new DatePickerDialog.OnDateSetListener()
        {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
            {

                button.setText(new StringBuilder().append(year).append("-")
                        .append((month + 1) < 10 ? "0" + (month + 1) : (month + 1))
                        .append("-")
                        .append((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth));

                Date_str = new MyTimeTool().setDateToString(year,month+1,dayOfMonth);
                //getDate(year,month,dayOfMonth);
            }
        },calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    public void getDate(int year, int month, int day)
    {
        Date_str = new StringBuilder()
                .append(year).append(":")
                .append(month).append(":")
                .append(day).toString();
    }

    public void onBack(View view)
    {
        super.onBackPressed();
    }
}
